<?php
use think\facade\Validate;

/**
 * Class PrivateSMS     隐私短信
 */
class PrivateSMS
{
    private $Html = '';
    public $PhoneList = '';
    public $Mssage = '';

    /** 初始化
     * PrivateSMS constructor.
     */
    function __construct(){
        $this->getPhone();
    }

    /**
     * 获取号码
     */
    function getPhone(){
        $this->Html = file_get_html('https://www.becmd.com/');
        foreach ($this->Html->find('div.col-md-5 h2') as $v){
            if (Validate::regex($v->plaintext,"^(((\+1)|(1))?[0-9]{10})|(((\+86)|(86))?[1][3-9][0-9]{9})$"))
                $this->PhoneList[] = 'c_'.$v->plaintext;
        }
//
    }

    /** 获取最近短信（10条）
     * @param $phone
     */
    /**
     *  tag – 返回html标签名
     *  innertext – 返回innerHTML
     *  outertext – 返回outerHTML
     *  plaintext – 返回html标签中的文本
     */
    function getMessage($phone){
        $this->Html =  file_get_html("https://www.becmd.com/receive-freesms-".$phone.".html");
        $number = 1;
        foreach ($this->Html->find('tr') as $v){
            if ($number > 3 and count($this->Mssage) < 10){
                $content['tel'] = $phone;
                try {
                    $content['msg'] = trim($v->find('td',3)->plaintext);
                    $timeArray = preg_split("/(\"|\")/",$v->find('td',2)->innertext);
                    $content['time'] = $timeArray[5];
                    $this->Mssage[] = $content;
                }catch (\think\Exception $e){
                    // dump($e->getMessage());
                }
            }
            $number += 1;
        }
        // 清理html对象
        $this->Html->clear();
    }
}